package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.CheckTypeEntity;
import com.hit.server.api.dto.CheckTypeQueryDTO;
import com.hit.server.api.dto.CheckTypeSaveDTO;
import com.hit.server.api.dto.CheckTypeUpdateDTO;
import com.hit.server.api.type.DiscernTypeEnums;
import com.hit.server.api.type.WarningTypeEnums;
import com.hit.server.api.vo.CheckTypeVO;
import com.hit.server.biz.mapper.CheckTypeMapper;
import com.hit.server.api.service.CheckTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 检测类型
 *
 * @author kehanjiang
 * @date 2025-04-15 11:21:18
 */
@Service
@RequiredArgsConstructor
public class CheckTypeServiceImpl extends ServiceImpl<CheckTypeMapper, CheckTypeEntity> implements CheckTypeService {

    private CheckTypeVO convertToVO(Object o) {
        if (o instanceof CheckTypeEntity) {
            CheckTypeVO vo = new CheckTypeVO();
            BeanUtil.copyProperties(o, vo);
            //设置其他参数
            Optional.ofNullable(vo.getDiscernType()).ifPresent(discernType -> {
                vo.setDiscernTypeCn(DiscernTypeEnums.getByCode(discernType).getDesc());
            });
            return vo;
        }
        return null;
    }

    private void preCheck(CheckTypeEntity entity, Boolean update) {
        Long id = entity.getId();
        String name = entity.getName();
        if (update) {//更新操作
            Assert.notNull(id, "id不可为空！");
        } else {
            Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<CheckTypeVO> getCheckTypePage(Page page, CheckTypeQueryDTO dto) {
        CheckTypeEntity entity = new CheckTypeEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        final String name = dto.getName();
        LambdaQueryWrapper<CheckTypeEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), CheckTypeEntity::getName, name);
        wrapper.orderByAsc(CheckTypeEntity::getId);
        return (Page<CheckTypeVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }

    @Override
    public CheckTypeVO getCheckTypeById(Long id) {
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }


    @Override
    public Boolean saveCheckType(CheckTypeSaveDTO dto) {
        CheckTypeEntity entity = new CheckTypeEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }

    @Override
    public Boolean updateCheckTypeById(CheckTypeUpdateDTO dto) {
        CheckTypeEntity entity = new CheckTypeEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }

    @Override
    public Boolean removeCheckTypeBatchByIds(List<Long> ids) {
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}